package pw.hais.main.main_app.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import pw.hais.http.Http;
import pw.hais.main.R;
import pw.hais.main.main_app.activity.AuthorActivity;
import pw.hais.main.main_app.activity.ChatRoomActivity;
import pw.hais.main.main_app.activity.HaiHanActivity;
import pw.hais.main.main_app.activity.PushServiceActivity;
import pw.hais.main.main_app.activity.UserActivity;
import pw.hais.main.main_app.adapter.MenuListAdapter;
import pw.hais.main.main_app.app.BaseFragment;
import pw.hais.main.main_app.app.HaisApplication;
import pw.hais.main.main_app.model.MenuModel;
import pw.hais.main.main_app.model.UserModel;

/**
 * Created by hais1992 on 2015/12/14.
 */
public class DrawerMenuFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private TextView text_username;
    private ImageView image_userphoto;
    private View layout_user_info;

    private ListView listview_menu;
    private List<MenuModel> menuModels;
    private MenuListAdapter menuListAdapter;


    @Override
    public int onCreateView() {
        return R.layout.activity_menu;
    }

    @Override
    public void onInitView(View view) {
        listview_menu = (ListView) view.findViewById(R.id.listview_menu);
        text_username = (TextView) view.findViewById(R.id.text_username);
        image_userphoto = (ImageView) view.findViewById(R.id.image_userphoto);
        layout_user_info = view.findViewById(R.id.layout_user_info);
        layout_user_info.setOnClickListener(this);

        menuModels = new ArrayList<>();
        menuModels.add(new MenuModel(R.drawable.ic_launcher,"聊天室", ChatRoomActivity.class));
        menuModels.add(new MenuModel(R.drawable.ic_launcher,"海涵天地", HaiHanActivity.class));
        menuModels.add(new MenuModel(R.drawable.ic_launcher,"短消息", PushServiceActivity.class));
        menuModels.add(new MenuModel(R.drawable.ic_launcher,"关于应用", AuthorActivity.class));

        menuListAdapter = new MenuListAdapter(menuModels, MenuListAdapter.ViewHolder.class);
        listview_menu.setAdapter(menuListAdapter);
        listview_menu.setOnItemClickListener(this);
    }

    @Override
    public void onLoadDateToView(View view) {

        if(HaisApplication.app.user!=null){
            UserModel user = HaisApplication.app.user;
            text_username.setText(user.username);
            if(user.userPhoto!=null)Http.displayImage(image_userphoto,user.userPhoto.url);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_user_info:
                startActivity(new Intent(context, UserActivity.class));
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       startActivity(new Intent(context,menuListAdapter.getItem(position).clazz));
    }
}
